package com.tongsys.server.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Data
@TableName("user")
@ApiModel(value = "User对象", description = "")
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable,UserDetails {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty("用户名称")
    @TableField("username")
    private String username;

    @TableField("password")
    private String password;

    @ApiModelProperty("真实可用")
    @TableField("real_name")
    private String realName;

    @ApiModelProperty("昵称")
    @TableField("nick_name")
    private String nickName;

    @TableField("department_id")
    private Integer departmentId;

    @ApiModelProperty("部门名称")
    @TableField("department_name")
    private String departmentName;

    @ApiModelProperty("性别 0 男  1 女")
    @TableField("gender")
    private Byte gender;

    @TableField("phone")
    private String phone;

    @TableField("email")
    private String email;

    @ApiModelProperty("头像")
    @TableField("avatar")
    private String avatar;

    @ApiModelProperty("是否是超级管理员  1 是 其他的不是")
    @TableField("is_admin")
    private String isAdmin;

    @TableField(value = "create_tm",fill = FieldFill.INSERT)
    private Date createTm;

    @TableField(value = "update_tm",fill = FieldFill.INSERT_UPDATE)
    private Date updateTm;

    @ApiModelProperty("是否删除 0正常  1 删除")
    @TableField("is_delete")
    @TableLogic
    private Byte isDelete;

    /********由于实现了UserDetail类，必须包含下面的字段，且赋值为 true*****************************************/

    @ApiModelProperty("账号是否过期 0 过期 1 未过期")
    @TableField("is_account_non_expired")
    private boolean isAccountNonExpired = true;

    @ApiModelProperty("账户是否被锁定 0过期  1未过期")
    @TableField("is_account_non_locked")
    private boolean isAccountNonLocked = true;

    @ApiModelProperty("密码是否过期  1 未过期  0 已过期")
    @TableField("is_credentials_non_expired")
    private boolean isCredentialsNonExpired = true;

    @ApiModelProperty("账户是否可用 1可用 0 不可用")
    @TableField("is_enabled")
    private boolean isEnabled = true;
    /**************************************************************************************************/

    //查询用户权限列表
    @TableField(exist = false)
    private List<String> permissionList;

    //自定义字段，用于存放权限 权限列表

    @TableField(exist = false)
    @JsonIgnore
    @JSONField(serialize = false) //序列化的时候 忽略该字段，避免在存入redis的时候报错
    Collection<SimpleGrantedAuthority> authorities;


    //实现接口中的方法,用户获取权限
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 不为空的时候直接返回
        if (authorities!=null){
            return authorities;
        }
        //将权限  封装成  SimpleGrantedAuthority 类型对象
        //过滤为空的数据，筛选返回 SimpleGrantedAuthority(对象) 并返回一个 SimpleGrantedAuthority 结合
        authorities= permissionList.stream().filter(item->item!=null).map((String str) -> new SimpleGrantedAuthority(str)).collect(Collectors.toList());
        //将封装后的对象 传递给security 框架
        return authorities;
    }
    /*****************************************************************************************************/
}
